const { notifyModel } = require('../../model');
const { hashCode } = require('../../services/utils');

module.exports = {
  getNotify: function (req, res) {
    const { org } = req.decode;
    const attributes = ['id', 'title', 'content', 'author', 'link', 'startTime', 'endTime', 'createdAt', 'updatedAt'];
    let where = { org: org };

    notifyModel.findByFilter(
      attributes,
      where
    ).then(function (result) {
      if (result.length === 0) {
        return res.send({
          code: 20000,
          data: {
            total: 0,
            items: []
          }
        })
      }
      let list = result.filter(item => {
        return (Date.now() >= new Date(item.startTime) && Date.now() <= new Date(item.endTime));
      });
      for (const item of list) {
        item['unique'] = hashCode(item.updatedAt + item.content + item.title)
      }
      return res.send({
        code: 20000,
        data: {
          total: list.length,
          items: list
        }
      });

    }).catch(function (err) {
      console.log(err);
      return res.send({
        code: 10000,
        message: '通知查询失败'
      });
    });
  }
};
